package com.tengfei.main;

/**
 * @author feige
 * @version v1.0
 * @date 2022-11-16-16:42
 * @description
 */
public class DuoYiLast {

    public static void main(String[] args) {
        ListNode head = addData();
        head = del4LastN(5, head);
        System.out.println("");
    }

    private static ListNode del4LastN(int idx, ListNode head) {
        ListNode node = head, temp = head;
        int count = 0;
        if (node != null) {
            while (true) {
                count++;
                if (node.next == null) {
                    break;
                }
                node = node.next;
            }

            for (int i = 0; i < count - idx - 1; i++) {
                temp = temp.next;
            }
            temp.next = temp.next.next;
            return head;
        } else {
            return null;
        }
    }

    private static ListNode addData() {
        ListNode node5 = new ListNode(5, null);
        ListNode node4 = new ListNode(4, node5);
        ListNode node3 = new ListNode(3, node4);
        ListNode node2 = new ListNode(2, node3);
        ListNode node1 = new ListNode(1, node2);
        return node1;
    }

}

class ListNode {
    public int value;
    public ListNode next;

    public ListNode(int value, ListNode next) {
        this.value = value;
        this.next = next;
    }
}
